package tree;

import java.util.*;

/**
 * @author le
 */
public class MinDistanceBetweenNodes
{
    public static void main(String[] args)
    {

    }

    public int minDiff(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if (root != null){
            queue.offer(root);
        }

        List<Integer> res = new ArrayList<>();
        while(!queue.isEmpty()){
            int size = queue.size();
            while(size-- > 0){
                TreeNode node = queue.poll();
                res.add(node.val);
                if (node.left != null){
                    queue.offer(node.left);
                }
                if (node.right != null){
                    queue.offer(node.right);
                }
            }
        }
        int min = Integer.MAX_VALUE;
        int[] tmp = new int[res.size()];
        int index = 0;
        for(int s :res){
            tmp[index] = s;
        }
        Arrays.sort(tmp);

        for (int i = 0;i < tmp.length-1;i++){
            if (tmp[i+1]-tmp[i] < min){
                min = tmp[i+1]-tmp[i];
            }
        }

        return min;
    }
}
